*****************************************************************************
* MAIN TABLE: BUILD OR BUY AND HUMAN CAPITAL (HC)
*****************************************************************************
clear 
* Load the main dataset
use "${output_stata}\main_regression.dta"

* Clear stored estimations
eststo clear

* Run regression with fixed effects using reghdfe
reghdfe buy HC_distance_s_brut_t1 /*
*/ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 if share > 0.01 & share < ., absorb(orig_dest_year) cluster(apgr_1_num code_entry_num) 

* Generate a sample indicator and store estimation results
gen sample = e(sample)
estimates store reg_1

* Add details about the fixed effects and controls
estadd local orig_dest_year "Yes"
estadd local orig_dest_size "No"
estadd local ctrl "Yes"
estadd local orig1_orig2_dest "No"
estadd local orig_dest "No"
estadd local sirtg_num "No"
estadd local year "No"

* Additional regressions with different fixed effects and controls
reghdfe buy HC_distance_s_brut_t1 /*
*/ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 if sample, absorb(orig_dest_size year) cluster(apgr_1_num code_entry_num) 
estimates store reg_3
estadd local orig_dest_year "No"
estadd local orig_dest_size "Yes"
estadd local ctrl "Yes"
estadd local orig1_orig2_dest "No"
estadd local orig_dest "No"
estadd local sirtg_num "No"
estadd local year "Yes"

* Another regression with different controls
reghdfe buy HC_distance_s_brut_t1 /*
*/ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 if sample, absorb(orig1_orig2_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_4
estadd local orig_dest_year "No"
estadd local orig_dest_size "No"
estadd local ctrl "Yes"
estadd local orig1_orig2_dest "Yes"
estadd local orig_dest "No"
estadd local sirtg_num "No"
estadd local year "Yes"

* Final regression using firm fixed effects
reghdfe buy HC_distance_s_brut_t1 /*
*/ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 /*
*/ if share > 0.01 & share < ., absorb(sirtg_num orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_5
estadd local orig_dest_year "No"
estadd local orig_dest_size "No"
estadd local ctrl "Yes"
estadd local orig1_orig2_dest "No"
estadd local orig_dest "Yes"
estadd local sirtg_num "Yes"
estadd local year "Yes"

* Save regression output in LaTeX format
esttab reg_1 reg_3 reg_4 reg_5 using "${export}\maintable.tex", /*
*/ drop(_cons) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig_dest_year orig_dest_size orig1_orig2_dest orig_dest sirtg_num year r2_a N, fmt(0 0 0 0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin $\times$ Entry $\times$ Year FE"' `"Sector of Origin $\times$ Size $\times$ Entry FE"' `"Sector of Origin (main) $\times$ Origin (second) $\times$ Entry FE"' `"Sector of Origin $\times$ Entry FE"' `"Firm FE"' `"Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"') /*
*/layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" /*
*/ "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"))


*****************************************************************************
* ROBUSTNESS: ALTERNATIVE ENTRY THRESHOLDS
*****************************************************************************
* Additional regressions with varying thresholds for the 'share' variable and exporting results

reghdfe buy HC_distance_s_brut_t1 /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1, absorb(orig_dest_year) cluster(apgr_1_num code_entry_num) 
estimates store rob_1
estadd local orig_dest_size "Yes"
estadd local ctrl "Yes"

reghdfe buy HC_distance_s_brut_t1 /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if share > 0.01, absorb(orig_dest_year) cluster(apgr_1_num code_entry_num) 
estimates store rob_2
estadd local orig_dest_size "Yes"
estadd local ctrl "Yes"

reghdfe buy HC_distance_s_brut_t1 /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if share > 0.05, absorb(orig_dest_year) cluster(apgr_1_num code_entry_num) 
estimates store rob_3
estadd local orig_dest_size "Yes"
estadd local ctrl "Yes"

reghdfe buy HC_distance_s_brut_t1 /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if share > 0.1, absorb(orig_dest_year) cluster(apgr_1_num code_entry_num) 
estimates store rob_4
estadd local orig_dest_size "Yes"
estadd local ctrl "Yes"

reghdfe buy HC_distance_s_brut_t1 /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if share > 0.01 [aw =sales], absorb(orig_dest_year) cluster(apgr_1_num code_entry_num) 
estimates store rob_5
estadd local orig_dest_size "Yes"
estadd local ctrl "Yes"

esttab rob_1 rob_2 rob_3 rob_4 rob_5 using "${export}\robustness_2.tex", /*
*/ keep(HC_distance*) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/ alignment(D{.}{.}{-1})  /*     
*/ title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/ collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig_dest_size r2_a N, fmt(0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin $\times$ Entry $\times$ Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"') /*
*/layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"))
